#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int vigenereCipherEncrypt(char *key, int keyLen , char* message , int messagelen , char* cipherText);
int vigenereCipherDecrypt(char *key, int keyLen , char* cipherText , int messagelen , char* messageText);

void keyPadding(char* key, int keyLen , int messageLen , char*  paddedKey);
void generateCipherText( char* message, char* paddedKey , int length, char* cipherText);
void generatemessageText( char* cipherText, char* paddedKey , int length, char* messageText);



int main(int argc, char* argv[])
{

//char key[] = "abcxyz";
//int keyLen = strlen(key); 
//char message[] ="abcdefghijklmnopqrstuvwxyz";

if(argc < 3)
{
    printf("\nUsage VGCipher MessageText Key\n");
    return -1;
}


char* message = argv[1];
char* key = argv[2];

int keyLen = strlen(key);

int messageLen = strlen(message);

char* cipherText = malloc(messageLen+1);
char* decryptedMessage = malloc(messageLen+1);

if(cipherText == NULL)
{
    printf("Memory allocation failure");
}


vigenereCipherEncrypt(key,keyLen,message,messageLen,cipherText);


vigenereCipherDecrypt(key,keyLen ,cipherText ,messageLen ,decryptedMessage);


free(cipherText);
free(decryptedMessage);
return 0;
}



int vigenereCipherEncrypt(char *key, int keyLen , char* message , int messageLen , char* cipherText)
{
    char* paddedKey = malloc(messageLen + 1);
    keyPadding(key,keyLen,messageLen,paddedKey);
    generateCipherText(message,paddedKey ,messageLen, cipherText); 
    printf("cipher text = %s\n",cipherText);

    free(paddedKey);

}


int vigenereCipherDecrypt(char *key, int keyLen , char* cipherText , int messageLen , char* messageText)
{
    
    char* paddedKey = malloc(messageLen + 1);
    keyPadding(key,keyLen,messageLen,paddedKey);
    generatemessageText( cipherText, paddedKey , messageLen, messageText);
    printf("message text = %s\n",messageText);
    free(paddedKey);
}



void keyPadding(char* key, int keyLen , int messageLen , char*  paddedKey)
{
    
    int i=0,k=0;

    if(keyLen <= messageLen)
    {
        for(i = 0 ; i < messageLen ; i++)
        {

            if( k == keyLen )
                 k=0;
           
            paddedKey[i] = key[k];
            k++;
        }
    }

    if( keyLen > messageLen)
    {
        for(i = 0 ; i < messageLen; i++)
           paddedKey[i] = key[i];        

    }
    paddedKey[i++] = '\0';

}



void generateCipherText( char* message, char* paddedKey , int length, char* cipherText)
{
int i;

char* originalMessage = malloc(strlen(message)+1);

if(! originalMessage)
{
    printf("Bad Malloc");
}
memcpy(originalMessage,message,length+1);
printf("original message = %s\n",originalMessage);

for(i=0 ; i < length ; i++)
{
   
    if(message[i] >= 65 && message[i]<=90 )
    {
        message[i] -= 64;
    }


    if(message[i] >= 97 && message[i]<= 122)
    {
        message[i] -= 96;
    }


    if(paddedKey[i] >= 65 && paddedKey[i]<=90 )
    {
        paddedKey[i] -= 64;
    }


    if(paddedKey[i] >= 97 && paddedKey[i]<= 122 )
    {
        paddedKey[i] -= 96;
    }


    cipherText[i] = ((message[i] + paddedKey[i]) % 26 ) ;
    


//    cipherText[i]++;
 
    if(originalMessage[i] >= 65 && originalMessage[i]<= 90)
    {   
        if( cipherText[i] == 0)
            cipherText[i] += 64 + 26;            
        else
            cipherText[i] += 64;
    }


    if(originalMessage[i] >= 97 && originalMessage[i]<= 122)
    {
        if( cipherText[i] == 0)
            cipherText[i] += 96 + 26;
        else
            cipherText[i] += 96;
    }

}

cipherText[i++] = '\0';

free(originalMessage);
}




void generatemessageText( char* cipherText, char* paddedKey , int length, char* messageText)
{
int i;

signed char* originalCipherText = malloc(length+1);

if(! originalCipherText)
{
    printf("Bad Malloc");
}

memcpy(originalCipherText,cipherText,length+1);

printf("original cipherText = %s\n",originalCipherText);

for(i=0 ; i < length ; i++)
{
   
    if(cipherText[i] >= 65 && cipherText[i]<=90 )
    {
        cipherText[i] -= 64;
    }


    if(cipherText[i] >= 97 && cipherText[i]<= 122)
    {
        cipherText[i] -= 96;
    }


    if(paddedKey[i] >= 65 && paddedKey[i]<=90 )
    {
        paddedKey[i] -= 64;
    }


    if(paddedKey[i] >= 97 && paddedKey[i]<= 122 )
    {
        paddedKey[i] -= 96;
    }

//    messageText[i] = (cipherText[i] - paddedKey[i]);
  
      messageText[i] = (cipherText[i] - paddedKey[i]) % 26;
        if(messageText[i] < 0)
            messageText[i] =  messageText[i] + 26;
   
    if(originalCipherText[i] >= 65 && originalCipherText[i]<= 90)
    {
        if(messageText[i] == 0)
            messageText[i] += 64 + 26;
        else
            messageText[i] += 64;
    }


    if(originalCipherText[i] >= 97 && originalCipherText[i]<= 122)
    {
        
        if(messageText[i] == 0)
            messageText[i] += 96 + 26;
        else
        messageText[i] += 96;
    }

}

messageText[i++] = '\0';

free(originalCipherText);
}



